package com.thinkit.bigdata.web.controller.system;

import com.google.common.base.Strings;
import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.web.model.result.HtmlResult;
import com.thinkit.bigdata.web.model.sys.SysFileServerStatus;
import com.thinkit.bigdata.web.model.sys.SysFileServerStatusExample;
import com.thinkit.bigdata.web.service.sys.SysFileServerStatusService;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/ftp")
public class SysFileServerStatusController {

    private static Logger logger = Logger.getLogger(SysFileServerStatusController.class);

    @Resource
    private SysFileServerStatusService service;

    @RequestMapping(value = "/get-ftp-by-page", method = RequestMethod.POST)
    public HtmlResult getDateByPage(@RequestBody Map map) {
        String logTxt = "【ftp文件查询】";
        logger.info(logTxt);
        int pageNum = (int) map.get("pageNum");
        int pageSize = (int) map.get("pageSize");
        String provincecode = (String) map.get("provincecode");
        String datePathBegin = (String) map.get("datePathBegin");
        String datePathEnd = (String) map.get("datePathEnd");
        logger.info(logTxt + "provincecode = " + provincecode);
        logger.info(logTxt + "datePathBegin = " + datePathBegin);
        logger.info(logTxt + "datePathEnd = " + datePathEnd);
        Page<SysFileServerStatus> page = new Page<>(pageNum+1, + pageSize);
        SysFileServerStatusExample example = new SysFileServerStatusExample();
        SysFileServerStatusExample.Criteria criteria = example.createCriteria();
        if (!Strings.isNullOrEmpty(provincecode)) {
            criteria.andProvincecodeEqualTo(provincecode);
        }
        if (!Strings.isNullOrEmpty(datePathBegin)) {
            logger.info("/" + datePathBegin.replaceAll("-", "/") + "/00");
            criteria.andDatepathGreaterThanOrEqualTo("/" + datePathBegin.replaceAll("-", "/") + "/00");
        }
        if (!Strings.isNullOrEmpty(datePathEnd)) {
            criteria.andDatepathLessThanOrEqualTo("/" + datePathEnd.replaceAll("-", "/") + "/24");
        }
        example.setOrderByClause("datePath desc, provinceCode desc");
        return service.selectByExampleWithBLOBs(page, example);
    }
}
